package com.jianyun.jyzs.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.jianyun.jyzs.bean.CrmSignDB;
import com.jianyun.jyzs.db.DatabaseHelper;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jianyun.jyzs.utils.RceLog;

/**
 * Created by mlli on 2017/8/23.
 */

public class CrmSignDao {
    private Context context;
    private Dao<CrmSignDB,Integer> crmDao;
    private DatabaseHelper helper;


    public CrmSignDao(Context context) {
        this.context = context;
        try {
            helper = DatabaseHelper.getHelper(context);
            crmDao = helper.getDao(CrmSignDB.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void insert(CrmSignDB crmSignDB){
        try {
            String time = crmSignDB.getTime();
            String time2 = time.split(" ")[0];
            crmSignDB.setSplittime(time2);
            crmDao.createOrUpdate(crmSignDB);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public List<CrmSignDB> getCrmSignList(String crmid,String userid,String enterpriseCode){
        try {
            QueryBuilder<CrmSignDB, Integer> builder = crmDao.queryBuilder();
            Where<CrmSignDB, Integer> where = builder.where().eq("userid", userid).and().eq("enterpriseCode", enterpriseCode).and().eq("crmid",crmid);
             return where.query();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<CrmSignDB> getCrmSignDate(String crmid,String userid,String enterpriseCode,String date){
        RceLog.d("lml","搜索日期"+date);
        List<CrmSignDB> list = new ArrayList<>();
        QueryBuilder<CrmSignDB, Integer> builder = crmDao.queryBuilder();
        try {
            builder.where().eq("userid", userid).and().eq("enterpriseCode", enterpriseCode).and().eq("crmid",crmid).and().eq("splittime",date);
            list = builder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<CrmSignDB> getAll() {
        List<CrmSignDB> list = new ArrayList<>();
        QueryBuilder<CrmSignDB, Integer> builder = crmDao.queryBuilder();
        try {
            list = builder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }
}
